package com.foreknow.elm.controller;

import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrderService;
import com.foreknow.elm.service.impl.OrderServiceImpl;
import com.foreknow.elm.util.CommonUtil;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;

public class OrdersController {
    /**
     * 根据用户编号、商家编号、订单总金额、送货地址编号向订单表中添加一条记录，
     * ​           并获取自动生成的订单编号，
     * ​           然后根据用户编号、商家编号从购物车表中查询所有数据，批量添加到订单明细表中，
     * ​           然后根据用户编号、商家编号删除购物车表中的数据。
     * @param req
     * @return
     * @throws Exception
     */
    public int createOrders(HttpServletRequest req) throws Exception{
        Orders orders = new Orders();
        orders.setUserId(req.getParameter("userId"));
        try {
            orders.setBusinessId(Integer.parseInt(req.getParameter("businessId")));
        }catch (Exception e){
            orders.setBusinessId(0);
        }
        try {
            orders.setDaId(Integer.parseInt(req.getParameter("daId")));
        } catch (Exception e){
            orders.setDaId(0);
        }
        try {
            orders.setOrderTotal(Double.parseDouble(req.getParameter("orderTotal")));
        } catch (Exception e){
            orders.setOrderTotal(0.0);
        }
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh-mm");
//        String datetime = sdf.format(new Date());
        orders.setOrderDate(CommonUtil.getCurrentDate());
        OrderService service = new OrderServiceImpl();
        int result = service.createOrders(orders);
        return result;
    }

    public Orders getOrdersById(HttpServletRequest req) throws Exception{
        int orderId = Integer.parseInt(req.getParameter("orderId"));
        OrderService service = new OrderServiceImpl();
        Orders orders = service.getOrdersById(orderId);
        return orders;
    }

    public List<Orders> listOrdersByUserId(HttpServletRequest req) throws Exception{
        String userId = req.getParameter("userId");
        OrderService service = new OrderServiceImpl();
        List<Orders> list = service.listOrdersByUserId(userId);
        return list;
    }


}
